package lifei.lifei.mybatis.test;

import lifei.lifei.mybatis.dao.SysLogDao;
import lifei.lifei.mybatis.pojo.SysLog;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author 小李
 * @version 1.0
 * @date 2021/9/23 14:44
 */
public class MyBatisTestDemo {
    SqlSession session;
    SqlSessionFactory factory;
    /**测试手动清除缓存*/
    @Test
    public void testTwoCache1() {
        //创建两个会话对象
        SqlSession session1 = factory.openSession();
        SqlSession session2 = factory.openSession();
        //获取接口的代理对象
        SysLogDao sysLogDao = session1.getMapper(SysLogDao.class);
        SysLog sysLog = sysLogDao.queryOne(35);
        System.out.println("第一次执行查询:" + sysLog);
        session1.close();
        //手动清除缓存
        session1.clearCache();
       //第二次缓存
        SysLogDao sysLogDao1 = session2.getMapper(SysLogDao.class);
        SysLog sysLog1 = sysLogDao1.queryOne(35);
        System.out.println("第二次执行查询:" + sysLog1);
        session2.close();
    }
    /**测试二级缓存*/
    @Test
    public void testTwoCache(){
        //创建两个会话对象
        SqlSession session1=factory.openSession();
        SqlSession session2=factory.openSession();
        //获取接口的代理对象
        SysLogDao sysLogDao=session1.getMapper(SysLogDao.class);
        SysLog sysLog=sysLogDao.queryOne(36);
        System.out.println("第一次执行查询:"+sysLog);
        session1.close();
        SysLogDao sysLogDao1=session2.getMapper(SysLogDao.class);
        SysLog sysLog1=sysLogDao1.queryOne(36);
        System.out.println("第二次执行查询:"+sysLog1);
        session2.close();
//        for(int i=1;i<=2;i++){
//            SysLog sysLog=sysLogDao.queryOne(38);
//            System.out.println(sysLog);
//        }
    }

    /**一级缓存失败的情况下之原因四：
     * 手动清除一级缓存*/
    @Test
    public void testCatchae4(){
        SysLogDao dao=session.getMapper(SysLogDao.class);
        dao.queryOne(34);
        //手动清除缓存
        session.clearCache();
        dao.queryOne(34);
    }
    /**一级缓存失效的情况原因三：
     * Sqlsession相同，两次查询之间执行了增删改操作*/
    @Test
    public void testCachae3(){
        SysLogDao dao=session.getMapper(SysLogDao.class);
        //先查询一次
        SysLog sysLog=dao.queryOne(35);
        //两次查询之间做一次更新操作
        SysLog s=new SysLog();
        s.setUsername("无忌哥哥").setId(34);
        dao.updateSysLog(s);
        //再查询一次
        dao.queryOne(35);


    }

    /**一级缓存失效的情况之Sqlsession*/
    @Test
    public void testCachae2(){
        SqlSession session1=factory.openSession();
        SysLogDao dao1=session1.getMapper(SysLogDao.class);
        SysLog sysLog1=dao1.queryOne(36);
        SqlSession session2=factory.openSession();
        SysLogDao dao2=session2.getMapper(SysLogDao.class);
        SysLog sysLog2=dao2.queryOne(36);
        System.out.println(sysLog1);
        System.out.println(sysLog2);
    }
    @Test
    public void testCache(){
        SysLogDao dao=session.getMapper(SysLogDao.class);
        for(int i=1;i<=2;i++){
            SysLog sysLog=dao.queryOne(38);
            System.out.println(sysLog);
            //SysLog sysLog2=dao.queryOne(36);
            //System.out.println(sysLog2);
        }
//        List<SysLog> list=dao.querAll();
       //SysLog sysLog=session.selectOne(null,38);
//        for(int i=1;i<=2;i++){
//            SysLog sysLog=
//                    session.selectOne("com.lifei.mybatis.dao.SysLogDao.queryAll",
//                            38);
//            System.out.println(sysLog);
//        }

    }
    @Test
    public void testdelectById(){
        Map<String,String> map=new HashMap<>();
        map.put("id", "30");
        SysLogDao dao=session.getMapper(SysLogDao.class);
        int rows=dao.deleteById(map);
        System.out.println("删除了"+rows+"条记录");
    }

    @Test
    public void testFindFiled(){
        Map<String,String> map=new HashMap<>();
        map.put("filed", "id,username,params,createdTime");
        SysLogDao dao=session.getMapper(SysLogDao.class);
        List<SysLog> list=dao.findByFiled(map);
        for (SysLog sysLog:list) {
            System.out.println(sysLog);
        }
    }

    @Before
    public void init() throws IOException {
        //读取配置文件
        InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
        //创建SqlSessionFactory工厂
        factory = new SqlSessionFactoryBuilder().build(is);
        //生产SqlSession对象
        session = factory.openSession();
    }
    @After
    public void destroy(){
        //手动提交事务
        session.commit();
        //释放资源
        session.close();
    }
    @Test
    public void testUpdateByParam(){
        String name="千山暮雪";
        String opt="大妖怪";
        Integer id=36;
        SysLogDao dao=session.getMapper(SysLogDao.class);
        int rows=dao.updateByParam(name, opt, id);
        System.out.println("影响了"+rows+"行记录");
    }
    @Test
    public void testUpdateById(){
        Map<String,Object> map=new HashMap<>();
        map.put("username", "好老公");
        map.put("operation", "赚钱操作");
        map.put("createdTime", new Date());
        map.put("id", 37);
        SysLogDao dao=session.getMapper(SysLogDao.class);
        int rows=dao.updateById(map);
        System.out.println("影响了"+rows+"行记录");
    }
}
